<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
    height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}

/* 分页 start */

.page {
    margin: 20px 0;
}
.ivu-page {
    padding-bottom: 150px;
} 

/* 分页 end */


</style>

<template>
    <div class="employee-manage">
        <product-title></product-title>
        <div class="wraper">
            <div class="search">
                <span>快速搜索</span>
                <Input v-model="searchForm.name" placeholder="请输入代理人姓名" style="width: 140px"></Input>
                <Button @click='search'>查询</Button>
            </div>
            <div class="detail">
                <div class="headline">
                    
                    <Button class="add-agent" @click='showAdd(-1)'>新增员工银行账户</Button>
                </div>
                <div class="clearfix">
                    <Table border :columns="columns_employee" :data="data_employee"></Table>
                    <Row class="page fr">
                        <Col span="24">
                            <Page :total="employeeTotal" show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
                        </Col>
                    </Row>
                </div>
            </div>
        </div>
        <Modal v-model="showAddModal" :mask-closable='closeable' width="600" title="添加/编辑员工银行账户信息" @on-ok="add" :loading="loading">
            <Row>
                <Col span="18" offset="1">
                <Form :model="employeeDetail" :ref="employeeDetail" :rules="ruleValidate" :label-width="120">
                    
                    <Form-item label="姓名：" prop="uid" required>
                        <Select v-model="employeeDetail.uid" style="width:306px"  @on-change="uidChange($event)" placeholder='请选择员工'>
                            <Option :value="key" v-for="(value,key) in employeeList"   :key="key" >{{value.name}}</Option>
                        </Select>
                    </Form-item>                                      
                    <Form-item label="身份证号：" prop="id_card" required>
                        <Input v-model="employeeDetail.id_card" placeholder="请输入身份证号"></Input>
                    </Form-item>
                    <Form-item label="银行卡号：" prop="bank_card" required>
                        <Input v-model="employeeDetail.bank_card" placeholder="请输入银行卡号"></Input>
                    </Form-item>
                    <Form-item label="开户行：" prop="bank_branch" required>
                        <Input v-model="employeeDetail.bank_branch" placeholder="请输入开户行"></Input>
                    </Form-item>   
                </Form>
                </Col>
            </Row>
        </Modal>


    </div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { getAllEmployeeById} from '../../api/employee'
import { cardInfoCreate , getCardInfoList , cardInfoUpdate , cardInfoDel} from '../../api/settlement'

export default {
    data() {
        return {
            cou:-1,
            checkModal:false,
            closeable: false,
            loading: true,
            page: 1,
            pagesize: 10,
            searchForm: {
                name: '',
                no: '',
                sex:'',
            },
            agentCurrent:{},
            employeeDetail: {
                uid:'',
                id_card:'',
                bank_card:'',
                bank_branch:''

            },
            showAddModal: false,
    
            employeeList:{

            },
            employeeTotal: 0,
            columns_employee: [
                {
                    title: '姓名',
                    key: 'user_name',
                    align: 'center',
                },
                {
                    title: '身份证号',
                    key: 'id_card',
                    align: 'center',
                },
                {
                    title: '银行卡号',
                    key: 'bank_card',
                    align: 'center',
                },
                {
                    title: '支行信息',
                    key: 'bank_branch',
                    align: 'center',
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.showAdd(params.index)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.delConfirm(params.index)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                },
            ],
            data_employee: [

            ],
            ruleValidate: {
                uid: [
                    { required: true, message: '姓名不能为空', trigger: 'change' }
                ],
                id_card:[
                    { required: true, message: '身份证不能为空', trigger: 'blur,change' }
                ],
                bank_card:[
                    { required: true, message: '银行卡号不能为空', trigger: 'blur' }
                ],
                bank_branch:[
                    { required: true, message: '开户行不能为空', trigger: 'blur' }
                ]
            },
        }
    },
    components: {
        productTitle
    },
    created() {
        this.getList()
        this.getAllEmployee()
    },
    methods: {
        uidChange(value){
            //console.log(value)
            this.employeeDetail.id_card = this.employeeList[value].id_number

        },
        //日期变化
        dateChange(date){
            console.log(date)
            this.employeeDetail.date = date
        },
        // 搜索
        search() {
            this.getList()           
        },
        // 获取员工列表
        getList() {
            let _params = {
                'p': this.page,
                'pagesize': this.pagesize,
                'name': this.searchForm.name,
            }
            new Promise((resolve, reject) => {
                getCardInfoList(_params).then(response => {
                    console.log(response.data);
                    if (response.data.err == 0) {
                        this.data_employee = response.data.data.list;
                        this.employeeTotal = response.data.data.total;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //获取全部员工
        getAllEmployee() {
            new Promise((resolve, reject) => {
                getAllEmployeeById().then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.employeeList = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },

        getDetail(param) {
            new Promise((resolve, reject) => {
                getEmployeeDetail(param).then(response => {
                    if (response.data.err == 0) {
                        this.employeeDetail = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 页码改变
        pageChange(page) {
            this.page = page
            this.getList()
        },
        // 每页条数改变
        pageSizeChange(pagesize) {
            this.pagesize = pagesize
            this.getList();
        },

        add() {
            this.$refs[this.employeeDetail].validate((valid) => {
                if (valid) {
                    if(this.cou == -1){
                        new Promise((resolve, reject) => {
                            cardInfoCreate(this.employeeDetail).then(response => {
                                if (response.data.err == 0) {
                                    this.showAddModal = false;
                                    this.loading = true;
                                    this.getList();
                                } else {
                                    this.$Message.error(response.data.info);
                                    setTimeout(() => {
                                        this.loading = false;
                                        this.$nextTick(() => {
                                            this.loading = true;
                                        });
                                    }, 2000);
                                }
                                resolve();
                            }).catch(error => {
                                reject(error);
                            });
                        });
                    }else{
                        new Promise((resolve, reject) => {
                            cardInfoUpdate(this.employeeDetail).then(response => {
                                if (response.data.err == 0) {
                                    this.showAddModal = false;
                                    this.loading = true;
                                    this.getList();
                                } else {
                                    this.$Message.error(response.data.info);
                                    setTimeout(() => {
                                        this.loading = false;
                                        this.$nextTick(() => {
                                            this.loading = true;
                                        });
                                    }, 2000);
                                }
                                resolve();
                            }).catch(error => {
                                reject(error);
                            });
                        });
                    }                   
                } else {
                    // 通过loading来阻止modal隐藏，然后手动修改loading来充值确定按钮
                    setTimeout(() => {
                        this.loading = false;
                        this.$nextTick(() => {
                            this.loading = true;
                        });
                    }, 2000);
                }
            })
        },
        del(param) {
            new Promise((resolve, reject) => {
                cardInfoDel(param).then(response => {
                    
                    if (response.data.err == 0) {
                        this.$Message.success(response.data.info);
                        this.getList();
                    }else{
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //添加or编辑代理人，打开modal
        showAdd(index) {
            this.showAddModal = !this.showAddModal;
            if (index != -1) {
                //this.getDetail(params)
                this.employeeDetail = Object.assign({}, this.data_employee[index] )
                this.employeeDetail.uid = this.employeeDetail.uid+''
                this.cou = 1
            } else {
                this.employeeDetail = {
                    uid:'',
                    id_card:'',
                    bank_card:'',
                    bank_branch:''
                }
                this.cou = -1
            }
        },
        // 删除时弹出对话框
        delConfirm(index) {
            this.$Modal.confirm({
                title: '提示',
                content: '是否要删除该代理人?',
                onOk: () => {
                    let _params = {
                        'id': this.data_employee[index].id
                    }
                    this.del(_params);
                },
            });
        },

    }
}
</script>
